{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DAY13 时间处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从各种来源和格式解析时间序列信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "dti = pd.to_datetime(\n",
    "    [\"1/1/2018\", \n",
    "     np.datetime64(\"2018-01-01\"), \n",
    "     datetime.datetime(2018, 1, 1)]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dti"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "dti = pd.date_range(\"2018-01-01\", periods=3, freq=\"H\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-01-01 00:00:00', '2018-01-01 01:00:00',\n",
       "               '2018-01-01 02:00:00'],\n",
       "              dtype='datetime64[ns]', freq='H')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dti"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['2018/01/01', '2018/01/01', '2018/01/01'], dtype='object')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dti.strftime('%Y/%m/%d')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "字符\t含义\t格式\n",
    "%a\tAbbreviated weekday name.\tSun, Mon, …\n",
    "%A\tFull weekday name.\tSunday, Monday, …\n",
    "%w\tWeekday as a decimal number.\t0, 1, …, 6\n",
    "%d\tDay of the month as a zero added decimal.\t01, 02, …, 31\n",
    "%-d\tDay of the month as a decimal number.\t1, 2, …, 30\n",
    "%b\tAbbreviated month name.\tJan, Feb, …, Dec\n",
    "%B\tFull month name.\tJanuary, February, …\n",
    "%m\tMonth as a zero added decimal number.\t01, 02, …, 12\n",
    "%-m\tMonth as a decimal number.\t1, 2, …, 12\n",
    "%y\tYear without century as a zero added decimal number.\t00, 01, …, 99\n",
    "%-y\tYear without century as a decimal number.\t0, 1, …, 99\n",
    "%Y\tYear with century as a decimal number.\t2013, 2019 etc.\n",
    "%H\tHour (24-hour clock) as a zero added decimal number.\t00, 01, …, 23\n",
    "%-H\tHour (24-hour clock) as a decimal number.\t0, 1, …, 23\n",
    "%I\tHour (12-hour clock) as a zero added decimal number.\t01, 02, …, 12\n",
    "%-I\tHour (12-hour clock) as a decimal number.\t1, 2, … 12\n",
    "%p\tLocale’s AM or PM.\tAM, PM\n",
    "%M\tMinute as a zero added decimal number.\t00, 01, …, 59\n",
    "%-M\tMinute as a decimal number.\t0, 1, …, 59\n",
    "%S\tSecond as a zero added decimal number.\t00, 01, …, 59\n",
    "%-S\tSecond as a decimal number.\t0, 1, …, 59\n",
    "%f\tMicrosecond as a decimal number, zero added on the left.\t000000 – 999999\n",
    "%z\tUTC offset in the form +HHMM or -HHMM.\t\n",
    "%Z\tTime zone name.\t\n",
    "%j\tDay of the year as a zero added decimal number.\t001, 002, …, 366\n",
    "%-j\tDay of the year as a decimal number.\t1, 2, …, 366\n",
    "%U\tWeek number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0.\t00, 01, …, 53\n",
    "%W\tWeek number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0.\t00, 01, …, 53\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\tushare\\stock\\trading.py:706: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  data = data.append(_get_k_data(url, dataflag,\n",
      "C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\tushare\\stock\\trading.py:706: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  data = data.append(_get_k_data(url, dataflag,\n",
      "C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\tushare\\stock\\trading.py:706: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  data = data.append(_get_k_data(url, dataflag,\n",
      "C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\tushare\\stock\\trading.py:706: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.\n",
      "  data = data.append(_get_k_data(url, dataflag,\n"
     ]
    }
   ],
   "source": [
    "df = ts.get_k_data('sh', start='2016-01-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "date = df.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.set_index('date')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-01-04</th>\n",
       "      <td>3536.59</td>\n",
       "      <td>3296.66</td>\n",
       "      <td>3538.69</td>\n",
       "      <td>3295.74</td>\n",
       "      <td>184418423.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-05</th>\n",
       "      <td>3196.65</td>\n",
       "      <td>3287.71</td>\n",
       "      <td>3328.14</td>\n",
       "      <td>3189.60</td>\n",
       "      <td>266882083.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-06</th>\n",
       "      <td>3291.19</td>\n",
       "      <td>3361.84</td>\n",
       "      <td>3362.97</td>\n",
       "      <td>3288.93</td>\n",
       "      <td>238886670.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-07</th>\n",
       "      <td>3309.66</td>\n",
       "      <td>3125.00</td>\n",
       "      <td>3309.66</td>\n",
       "      <td>3115.89</td>\n",
       "      <td>70569123.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-08</th>\n",
       "      <td>3194.63</td>\n",
       "      <td>3186.41</td>\n",
       "      <td>3235.45</td>\n",
       "      <td>3056.88</td>\n",
       "      <td>286440822.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-07-26</th>\n",
       "      <td>3254.19</td>\n",
       "      <td>3277.44</td>\n",
       "      <td>3282.41</td>\n",
       "      <td>3246.04</td>\n",
       "      <td>259468676.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-07-27</th>\n",
       "      <td>3271.78</td>\n",
       "      <td>3275.76</td>\n",
       "      <td>3282.57</td>\n",
       "      <td>3265.73</td>\n",
       "      <td>249131485.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-07-28</th>\n",
       "      <td>3287.50</td>\n",
       "      <td>3282.58</td>\n",
       "      <td>3305.71</td>\n",
       "      <td>3277.11</td>\n",
       "      <td>288055056.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-07-29</th>\n",
       "      <td>3282.81</td>\n",
       "      <td>3253.24</td>\n",
       "      <td>3294.80</td>\n",
       "      <td>3246.37</td>\n",
       "      <td>307331047.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-08-01</th>\n",
       "      <td>3246.62</td>\n",
       "      <td>3259.96</td>\n",
       "      <td>3264.30</td>\n",
       "      <td>3225.55</td>\n",
       "      <td>292204805.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1600 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               open    close     high      low       volume code\n",
       "date                                                            \n",
       "2016-01-04  3536.59  3296.66  3538.69  3295.74  184418423.0   sh\n",
       "2016-01-05  3196.65  3287.71  3328.14  3189.60  266882083.0   sh\n",
       "2016-01-06  3291.19  3361.84  3362.97  3288.93  238886670.0   sh\n",
       "2016-01-07  3309.66  3125.00  3309.66  3115.89   70569123.0   sh\n",
       "2016-01-08  3194.63  3186.41  3235.45  3056.88  286440822.0   sh\n",
       "...             ...      ...      ...      ...          ...  ...\n",
       "2022-07-26  3254.19  3277.44  3282.41  3246.04  259468676.0   sh\n",
       "2022-07-27  3271.78  3275.76  3282.57  3265.73  249131485.0   sh\n",
       "2022-07-28  3287.50  3282.58  3305.71  3277.11  288055056.0   sh\n",
       "2022-07-29  3282.81  3253.24  3294.80  3246.37  307331047.0   sh\n",
       "2022-08-01  3246.62  3259.96  3264.30  3225.55  292204805.0   sh\n",
       "\n",
       "[1600 rows x 6 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引和筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-01-04</th>\n",
       "      <td>3536.59</td>\n",
       "      <td>3296.66</td>\n",
       "      <td>3538.69</td>\n",
       "      <td>3295.74</td>\n",
       "      <td>184418423.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-05</th>\n",
       "      <td>3196.65</td>\n",
       "      <td>3287.71</td>\n",
       "      <td>3328.14</td>\n",
       "      <td>3189.60</td>\n",
       "      <td>266882083.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-06</th>\n",
       "      <td>3291.19</td>\n",
       "      <td>3361.84</td>\n",
       "      <td>3362.97</td>\n",
       "      <td>3288.93</td>\n",
       "      <td>238886670.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-07</th>\n",
       "      <td>3309.66</td>\n",
       "      <td>3125.00</td>\n",
       "      <td>3309.66</td>\n",
       "      <td>3115.89</td>\n",
       "      <td>70569123.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-08</th>\n",
       "      <td>3194.63</td>\n",
       "      <td>3186.41</td>\n",
       "      <td>3235.45</td>\n",
       "      <td>3056.88</td>\n",
       "      <td>286440822.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-25</th>\n",
       "      <td>3296.21</td>\n",
       "      <td>3280.46</td>\n",
       "      <td>3312.30</td>\n",
       "      <td>3270.44</td>\n",
       "      <td>146893635.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-26</th>\n",
       "      <td>3277.84</td>\n",
       "      <td>3306.12</td>\n",
       "      <td>3307.30</td>\n",
       "      <td>3274.33</td>\n",
       "      <td>142434501.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-27</th>\n",
       "      <td>3302.46</td>\n",
       "      <td>3275.78</td>\n",
       "      <td>3307.08</td>\n",
       "      <td>3270.35</td>\n",
       "      <td>162674890.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-28</th>\n",
       "      <td>3272.29</td>\n",
       "      <td>3296.38</td>\n",
       "      <td>3304.10</td>\n",
       "      <td>3263.73</td>\n",
       "      <td>175371670.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-29</th>\n",
       "      <td>3295.25</td>\n",
       "      <td>3307.17</td>\n",
       "      <td>3308.22</td>\n",
       "      <td>3292.77</td>\n",
       "      <td>141586836.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>488 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               open    close     high      low       volume code\n",
       "date                                                            \n",
       "2016-01-04  3536.59  3296.66  3538.69  3295.74  184418423.0   sh\n",
       "2016-01-05  3196.65  3287.71  3328.14  3189.60  266882083.0   sh\n",
       "2016-01-06  3291.19  3361.84  3362.97  3288.93  238886670.0   sh\n",
       "2016-01-07  3309.66  3125.00  3309.66  3115.89   70569123.0   sh\n",
       "2016-01-08  3194.63  3186.41  3235.45  3056.88  286440822.0   sh\n",
       "...             ...      ...      ...      ...          ...  ...\n",
       "2017-12-25  3296.21  3280.46  3312.30  3270.44  146893635.0   sh\n",
       "2017-12-26  3277.84  3306.12  3307.30  3274.33  142434501.0   sh\n",
       "2017-12-27  3302.46  3275.78  3307.08  3270.35  162674890.0   sh\n",
       "2017-12-28  3272.29  3296.38  3304.10  3263.73  175371670.0   sh\n",
       "2017-12-29  3295.25  3307.17  3308.22  3292.77  141586836.0   sh\n",
       "\n",
       "[488 rows x 6 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['2016':'2018']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-01-04</th>\n",
       "      <td>3536.59</td>\n",
       "      <td>3296.66</td>\n",
       "      <td>3538.69</td>\n",
       "      <td>3295.74</td>\n",
       "      <td>184418423.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-05</th>\n",
       "      <td>3196.65</td>\n",
       "      <td>3287.71</td>\n",
       "      <td>3328.14</td>\n",
       "      <td>3189.60</td>\n",
       "      <td>266882083.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-06</th>\n",
       "      <td>3291.19</td>\n",
       "      <td>3361.84</td>\n",
       "      <td>3362.97</td>\n",
       "      <td>3288.93</td>\n",
       "      <td>238886670.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-07</th>\n",
       "      <td>3309.66</td>\n",
       "      <td>3125.00</td>\n",
       "      <td>3309.66</td>\n",
       "      <td>3115.89</td>\n",
       "      <td>70569123.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-08</th>\n",
       "      <td>3194.63</td>\n",
       "      <td>3186.41</td>\n",
       "      <td>3235.45</td>\n",
       "      <td>3056.88</td>\n",
       "      <td>286440822.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-11</th>\n",
       "      <td>3131.85</td>\n",
       "      <td>3016.70</td>\n",
       "      <td>3166.22</td>\n",
       "      <td>3016.70</td>\n",
       "      <td>271643691.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-12</th>\n",
       "      <td>3026.16</td>\n",
       "      <td>3022.86</td>\n",
       "      <td>3047.66</td>\n",
       "      <td>2978.46</td>\n",
       "      <td>207659622.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-13</th>\n",
       "      <td>3041.11</td>\n",
       "      <td>2949.60</td>\n",
       "      <td>3059.01</td>\n",
       "      <td>2949.29</td>\n",
       "      <td>194282106.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-14</th>\n",
       "      <td>2874.05</td>\n",
       "      <td>3007.65</td>\n",
       "      <td>3012.29</td>\n",
       "      <td>2867.55</td>\n",
       "      <td>212905644.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-15</th>\n",
       "      <td>2988.05</td>\n",
       "      <td>2900.97</td>\n",
       "      <td>3001.71</td>\n",
       "      <td>2883.87</td>\n",
       "      <td>198721697.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-18</th>\n",
       "      <td>2847.54</td>\n",
       "      <td>2913.84</td>\n",
       "      <td>2945.45</td>\n",
       "      <td>2844.70</td>\n",
       "      <td>164699694.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-19</th>\n",
       "      <td>2914.41</td>\n",
       "      <td>3007.74</td>\n",
       "      <td>3012.07</td>\n",
       "      <td>2906.40</td>\n",
       "      <td>205279703.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-20</th>\n",
       "      <td>2993.01</td>\n",
       "      <td>2976.69</td>\n",
       "      <td>3016.28</td>\n",
       "      <td>2951.92</td>\n",
       "      <td>216505474.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-21</th>\n",
       "      <td>2934.39</td>\n",
       "      <td>2880.48</td>\n",
       "      <td>2998.79</td>\n",
       "      <td>2880.08</td>\n",
       "      <td>191675668.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-22</th>\n",
       "      <td>2911.11</td>\n",
       "      <td>2916.56</td>\n",
       "      <td>2931.36</td>\n",
       "      <td>2851.73</td>\n",
       "      <td>159810734.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-25</th>\n",
       "      <td>2934.08</td>\n",
       "      <td>2938.51</td>\n",
       "      <td>2955.78</td>\n",
       "      <td>2911.83</td>\n",
       "      <td>153039057.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-26</th>\n",
       "      <td>2907.72</td>\n",
       "      <td>2749.79</td>\n",
       "      <td>2911.99</td>\n",
       "      <td>2743.84</td>\n",
       "      <td>210836054.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-27</th>\n",
       "      <td>2756.08</td>\n",
       "      <td>2735.56</td>\n",
       "      <td>2768.77</td>\n",
       "      <td>2638.30</td>\n",
       "      <td>215722046.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-28</th>\n",
       "      <td>2711.16</td>\n",
       "      <td>2655.66</td>\n",
       "      <td>2740.54</td>\n",
       "      <td>2647.49</td>\n",
       "      <td>171120280.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-01-29</th>\n",
       "      <td>2652.85</td>\n",
       "      <td>2737.60</td>\n",
       "      <td>2755.37</td>\n",
       "      <td>2649.79</td>\n",
       "      <td>186673106.0</td>\n",
       "      <td>sh</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               open    close     high      low       volume code\n",
       "date                                                            \n",
       "2016-01-04  3536.59  3296.66  3538.69  3295.74  184418423.0   sh\n",
       "2016-01-05  3196.65  3287.71  3328.14  3189.60  266882083.0   sh\n",
       "2016-01-06  3291.19  3361.84  3362.97  3288.93  238886670.0   sh\n",
       "2016-01-07  3309.66  3125.00  3309.66  3115.89   70569123.0   sh\n",
       "2016-01-08  3194.63  3186.41  3235.45  3056.88  286440822.0   sh\n",
       "2016-01-11  3131.85  3016.70  3166.22  3016.70  271643691.0   sh\n",
       "2016-01-12  3026.16  3022.86  3047.66  2978.46  207659622.0   sh\n",
       "2016-01-13  3041.11  2949.60  3059.01  2949.29  194282106.0   sh\n",
       "2016-01-14  2874.05  3007.65  3012.29  2867.55  212905644.0   sh\n",
       "2016-01-15  2988.05  2900.97  3001.71  2883.87  198721697.0   sh\n",
       "2016-01-18  2847.54  2913.84  2945.45  2844.70  164699694.0   sh\n",
       "2016-01-19  2914.41  3007.74  3012.07  2906.40  205279703.0   sh\n",
       "2016-01-20  2993.01  2976.69  3016.28  2951.92  216505474.0   sh\n",
       "2016-01-21  2934.39  2880.48  2998.79  2880.08  191675668.0   sh\n",
       "2016-01-22  2911.11  2916.56  2931.36  2851.73  159810734.0   sh\n",
       "2016-01-25  2934.08  2938.51  2955.78  2911.83  153039057.0   sh\n",
       "2016-01-26  2907.72  2749.79  2911.99  2743.84  210836054.0   sh\n",
       "2016-01-27  2756.08  2735.56  2768.77  2638.30  215722046.0   sh\n",
       "2016-01-28  2711.16  2655.66  2740.54  2647.49  171120280.0   sh\n",
       "2016-01-29  2652.85  2737.60  2755.37  2649.79  186673106.0   sh"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['2016-01':'2016-02']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "date_dic = {\n",
    "    '年':pd.to_datetime(date).dt.year,\n",
    "    '月':pd.to_datetime(date).dt.month,\n",
    "    '日':pd.to_datetime(date).dt.day,\n",
    "    '时':pd.to_datetime(date).dt.hour,\n",
    "    '分':pd.to_datetime(date).dt.minute,\n",
    "    '秒':pd.to_datetime(date).dt.second,\n",
    "    '微秒':pd.to_datetime(date).dt.microsecond,\n",
    "    '纳秒':pd.to_datetime(date).dt.nanosecond,\n",
    "    '返回日期':pd.to_datetime(date).dt.date,\n",
    "    '返回时间':pd.to_datetime(date).dt.time,\n",
    "    '年序日':pd.to_datetime(date).dt.dayofyear,\n",
    "    '周':pd.to_datetime(date).dt.isocalendar().week,\n",
    "    '周中的第几天(法1)':pd.to_datetime(date).dt.dayofweek,\n",
    "    '周中的第几天(法2)':pd.to_datetime(date).dt.weekday,\n",
    "    '周中的星期几':pd.to_datetime(date).dt.day_name(),\n",
    "    '季度':pd.to_datetime(date).dt.quarter,\n",
    "    '一个月中有多少天':pd.to_datetime(date).dt.days_in_month,\n",
    "    '是否月初第一天':pd.to_datetime(date).dt.is_month_start,\n",
    "    '是否月末最后一天':pd.to_datetime(date).dt.is_month_end,\n",
    "    '是否季度的最开始':pd.to_datetime(date).dt.is_quarter_start,\n",
    "    '是否季度的最后一个':pd.to_datetime(date).dt.is_quarter_end,\n",
    "    '是否年初第一天':pd.to_datetime(date).dt.is_year_start,\n",
    "    '是否年末第一天':pd.to_datetime(date).dt.is_year_end,\n",
    "}\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "date_df = pd.DataFrame(date_dic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>年</th>\n",
       "      <th>月</th>\n",
       "      <th>日</th>\n",
       "      <th>时</th>\n",
       "      <th>分</th>\n",
       "      <th>秒</th>\n",
       "      <th>微秒</th>\n",
       "      <th>纳秒</th>\n",
       "      <th>返回日期</th>\n",
       "      <th>返回时间</th>\n",
       "      <th>...</th>\n",
       "      <th>周中的第几天(法2)</th>\n",
       "      <th>周中的星期几</th>\n",
       "      <th>季度</th>\n",
       "      <th>一个月中有多少天</th>\n",
       "      <th>是否月初第一天</th>\n",
       "      <th>是否月末最后一天</th>\n",
       "      <th>是否季度的最开始</th>\n",
       "      <th>是否季度的最后一个</th>\n",
       "      <th>是否年初第一天</th>\n",
       "      <th>是否年末第一天</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2016</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016-01-04</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>Monday</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2016</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016-01-05</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>Tuesday</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2016</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016-01-06</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>Wednesday</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2016</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016-01-07</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2016</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016-01-08</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>Friday</td>\n",
       "      <td>1</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1595</th>\n",
       "      <td>2022</td>\n",
       "      <td>7</td>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2022-07-26</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>Tuesday</td>\n",
       "      <td>3</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1596</th>\n",
       "      <td>2022</td>\n",
       "      <td>7</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2022-07-27</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>Wednesday</td>\n",
       "      <td>3</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1597</th>\n",
       "      <td>2022</td>\n",
       "      <td>7</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2022-07-28</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>Thursday</td>\n",
       "      <td>3</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1598</th>\n",
       "      <td>2022</td>\n",
       "      <td>7</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2022-07-29</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>Friday</td>\n",
       "      <td>3</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1599</th>\n",
       "      <td>2022</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2022-08-01</td>\n",
       "      <td>00:00:00</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>Monday</td>\n",
       "      <td>3</td>\n",
       "      <td>31</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1600 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         年  月   日  时  分  秒  微秒  纳秒        返回日期      返回时间  ...  周中的第几天(法2)  \\\n",
       "0     2016  1   4  0  0  0   0   0  2016-01-04  00:00:00  ...           0   \n",
       "1     2016  1   5  0  0  0   0   0  2016-01-05  00:00:00  ...           1   \n",
       "2     2016  1   6  0  0  0   0   0  2016-01-06  00:00:00  ...           2   \n",
       "3     2016  1   7  0  0  0   0   0  2016-01-07  00:00:00  ...           3   \n",
       "4     2016  1   8  0  0  0   0   0  2016-01-08  00:00:00  ...           4   \n",
       "...    ... ..  .. .. .. ..  ..  ..         ...       ...  ...         ...   \n",
       "1595  2022  7  26  0  0  0   0   0  2022-07-26  00:00:00  ...           1   \n",
       "1596  2022  7  27  0  0  0   0   0  2022-07-27  00:00:00  ...           2   \n",
       "1597  2022  7  28  0  0  0   0   0  2022-07-28  00:00:00  ...           3   \n",
       "1598  2022  7  29  0  0  0   0   0  2022-07-29  00:00:00  ...           4   \n",
       "1599  2022  8   1  0  0  0   0   0  2022-08-01  00:00:00  ...           0   \n",
       "\n",
       "         周中的星期几  季度  一个月中有多少天 是否月初第一天  是否月末最后一天  是否季度的最开始  是否季度的最后一个  是否年初第一天  \\\n",
       "0        Monday   1        31   False     False     False      False    False   \n",
       "1       Tuesday   1        31   False     False     False      False    False   \n",
       "2     Wednesday   1        31   False     False     False      False    False   \n",
       "3      Thursday   1        31   False     False     False      False    False   \n",
       "4        Friday   1        31   False     False     False      False    False   \n",
       "...         ...  ..       ...     ...       ...       ...        ...      ...   \n",
       "1595    Tuesday   3        31   False     False     False      False    False   \n",
       "1596  Wednesday   3        31   False     False     False      False    False   \n",
       "1597   Thursday   3        31   False     False     False      False    False   \n",
       "1598     Friday   3        31   False     False     False      False    False   \n",
       "1599     Monday   3        31    True     False     False      False    False   \n",
       "\n",
       "      是否年末第一天  \n",
       "0       False  \n",
       "1       False  \n",
       "2       False  \n",
       "3       False  \n",
       "4       False  \n",
       "...       ...  \n",
       "1595    False  \n",
       "1596    False  \n",
       "1597    False  \n",
       "1598    False  \n",
       "1599    False  \n",
       "\n",
       "[1600 rows x 23 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saturday\n",
      "<BusinessHour: BH=10:00-17:00>\n",
      "2020-06-08 10:00:00\n",
      "2020-06-08 11:00:00\n",
      "2020-06-05 17:00:00\n",
      "2020-06-05 00:00:00\n",
      "2020-06-04 00:00:00\n",
      "2020-05-18 00:00:00\n",
      "2020-05-01 00:00:00\n"
     ]
    }
   ],
   "source": [
    "ts = pd.Timestamp('2020-06-06 00:00:00')\n",
    "print(ts.day_name())\n",
    "# 'Saturday'\n",
    "\n",
    "# 定义一个工作小时偏移，默认是周一到周五 9-17 点，我们从 10点开始\n",
    "offset = pd.offsets.BusinessHour(start='10:00')\n",
    "print(offset)\n",
    "# 向前偏移一个工作小时，是一个周一，跳过了周日\n",
    "print(offset.rollforward(ts))\n",
    "# Timestamp('2020-06-08 10:00:00')\n",
    "\n",
    "# 向前偏移至最近的工作日，小时也会增加\n",
    "print(ts + offset)\n",
    "# Timestamp('2020-06-08 11:00:00')\n",
    "\n",
    "# 向后偏移，会在周五下班前的一个小时\n",
    "print(offset.rollback(ts))\n",
    "# Timestamp('2020-06-05 17:00:00')\n",
    "\n",
    "print(ts - pd.offsets.Day(1)) # 昨日\n",
    "print(ts - pd.offsets.Day(2)) # 前日\n",
    "print(ts - pd.offsets.Week(weekday=0) - pd.offsets.Day(14)) # 上周一\n",
    "print(ts - pd.offsets.MonthEnd() - pd.offsets.MonthBegin()) # 上月一日"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2022, 1, 20, 9, 0)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = datetime.datetime(2022, 1, 20, 9, 0);d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2022-05-25 09:00:00')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + pd.offsets.DateOffset(months=4, days=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2022-01-06 09:00:00')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d - 10 * pd.offsets.BDay()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2022-01-31 09:00:00')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d + pd.offsets.BMonthEnd()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
